package org.design.environmentprotection.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.design.environmentprotection.R;

public class BottomBar extends LinearLayout implements View.OnClickListener {


    private Context mContext;

    private View view;

    private final int NUMBER = 4;

    private int[] icon = new int[]{R.drawable.info, R.drawable.activity, R.drawable.talk, R.drawable.my};
    private int[] icon_fill = new int[]{R.drawable.info_fill, R.drawable.activity_fill, R.drawable.tall_fill, R.drawable.my_fill};
    private String[] texts = new String[]{"资讯", "活动", "论坛", "我的"};
    private TextView[] textViews=new TextView[NUMBER];
    private int[] colors = new int[]{R.color.A3, R.color.A4};

    private ImageView[] imageViews = new ImageView[NUMBER];

    private OnBottomBarClickListener onBottomBarClickListener;

    public interface OnBottomBarClickListener {
        void onBottomBarClick(int index);
    }

    public BottomBar(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public void setOnBottomBarClickListener(OnBottomBarClickListener onBottomBarClickListener) {
        this.onBottomBarClickListener = onBottomBarClickListener;
    }


    private void init() {

        DisplayMetrics dm = getResources().getDisplayMetrics();
        final int screenHeigth = dm.heightPixels;

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, screenHeigth/18);
        setOrientation(HORIZONTAL);
        setLayoutParams(layoutParams);
        ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                BottomBar.this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                BottomBar.this.getHeight();

                for (int i = 0; i < NUMBER; i++) {

                    LinearLayout linearLayout = new LinearLayout(mContext);
                    linearLayout.setOrientation(VERTICAL);
                    linearLayout.setGravity(Gravity.CENTER);
                    LayoutParams layoutParams = new LayoutParams(BottomBar.this.getWidth() / 4, LayoutParams.MATCH_PARENT);
                    linearLayout.setLayoutParams(layoutParams);
                    imageViews[i] = new ImageView(mContext);
                    imageViews[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    imageViews[i].setImageResource(icon[i]);
                    linearLayout.addView(imageViews[i]);
                    textViews[i] = new TextView(mContext);
                    textViews[i].setText(texts[i]);
                    textViews[i].setTextSize(15);
                    textViews[i].setGravity(Gravity.CENTER_HORIZONTAL);
                    textViews[i].setLayoutParams(layoutParams);
                    linearLayout.addView(textViews[i]);
                    linearLayout.setTag(i);
                    linearLayout.setOnClickListener(BottomBar.this);
                    BottomBar.this.addView(linearLayout);
                }
                select(0);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int tag = (int) view.getTag();
        select(tag);
        if (onBottomBarClickListener != null) {
            onBottomBarClickListener.onBottomBarClick(tag);
        }
    }

    public void select(int index) {
        for (int i = 0; i < NUMBER; i++) {
            if (index == i) {
                ColorStateList csl = getResources().getColorStateList(colors[0]);
                textViews[i].setTextColor(csl);
                imageViews[i].setImageResource(icon_fill[i]);
            } else {
                ColorStateList csl = getResources().getColorStateList(colors[1]);
                textViews[i].setTextColor(csl);
                imageViews[i].setImageResource(icon[i]);
            }
        }
    }
}
